//
//  OrderInfoViewController.m
//  xpkc
//
//  Created by TopSageMacMini on 14/12/19.
//  Copyright (c) 2014年 wanmeizty. All rights reserved.
//

#import "OrderInfoViewController.h"
#import "DecorateLine.h"
#import "MBProgressHUD.h"
#import "Order.h"
#import "DataSigner.h"
#import <AlipaySDK/AlipaySDK.h>
#import "APAuthV2Info.h"
#import "OrderReplyViewController.h"

@interface OrderInfoViewController ()

@end

@implementation OrderInfoViewController
{
    UILabel *numberLabel;
    UILabel *courseNameLabel;
    UILabel *priceLabel;
    UILabel *nameLabel;
    UILabel *phoneLabel;
    UILabel *addressLabel;
    UILabel *titleLabel;
    UIButton *payButton;
    UILabel *priceTit;
    UILabel * payLabel;
    UILabel * moneyLabel;
    UIButton * makeButton;
    UIButton * sayButton;
    UILabel * label3;
    UILabel *freeLabel ;
    UILabel *subCourerLabel;
    NSString * payNumber;
    
    
    UILabel * label4;
    UILabel * label5;
    UILabel * label6;
    UILabel * label7;
    UILabel * label8;
    NSString * productNames;
    NSMutableArray * productArray;
   
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.navigationItem.title = @"订单详情";
        [self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                 [UIColor colorWithRed:255/255.0f green:159/255.f blue:0/255.f alpha:1], NSForegroundColorAttributeName,
                                                 nil] forState:UIControlStateSelected];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    productArray = [NSMutableArray array];
    [self configUI];
    [self createData];
}

/**
 *   视图布局
 */
- (void)configUI {
    
    // 整个scrollView内容的高度
    CGFloat bsvH = 0;
    
    UIScrollView * bigScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    
    [self.view addSubview:bigScrollView];
    
    // 承载各种控件的视图  //SCREEN_HEIGHT/6
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    //    [bgView setBackgroundColor:RGBCOLOR(231, 236, 237)];
    [bgView setBackgroundColor:[UIColor whiteColor]];
    [bigScrollView addSubview:bgView];
    
    bsvH += 30;
    
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 5, 80, 25)];
    [titleLabel setTextAlignment:NSTextAlignmentLeft];
    [titleLabel setFont:[UIFont systemFontOfSize:17]];
    [bgView addSubview:titleLabel];
    
    
    
    numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-200, 5, 200, 25)];
    numberLabel.text=[NSString stringWithFormat:@"订单号:%@",self.ordelModel.oid];
    
    [numberLabel setTextAlignment:NSTextAlignmentCenter];
    [numberLabel setFont:[UIFont systemFontOfSize:14]];
    [numberLabel setTextColor:[UIColor darkGrayColor]];
    [bgView addSubview:numberLabel];
    
    
    // **************各种商品展示*****************//--->Tiyon
    
    //    bsvH += 10;
    
    CGFloat oneOrderH = 70;
    
    int arrayCount = self.ordelModel.course.count;
    
    UIView * goodsView = [[UIView alloc] initWithFrame:CGRectMake(0, bsvH , SCREEN_WIDTH,60+ oneOrderH * arrayCount)];
    goodsView.backgroundColor = [UIColor whiteColor];
    [bigScrollView addSubview:goodsView];
    goodsView.userInteractionEnabled = YES;
    
    bsvH += 10+ (oneOrderH * arrayCount -1) + 50;
    
    
    int k = 0;
    
    //    UILabel *lineLabel = [[UILabel alloc] init];
    //    [lineLabel setFrame:CGRectMake(0, 10, SCREEN_WIDTH-20, 2)];
    //    [lineLabel setBackgroundColor:RGBCOLOR(231, 236, 237)];
    //    [goodsView addSubview:lineLabel];
    
    for (NSDictionary * dic in self.ordelModel.course) {
        
        
        UILabel * TiyoncourseNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 40 + k * oneOrderH, SCREEN_WIDTH-40, 20)];
        TiyoncourseNameLabel.numberOfLines=0;
        TiyoncourseNameLabel.adjustsFontSizeToFitWidth=YES;
        [TiyoncourseNameLabel setTextAlignment:NSTextAlignmentLeft];
        [TiyoncourseNameLabel setTextColor:[UIColor darkGrayColor]];
        [TiyoncourseNameLabel setFont:[UIFont systemFontOfSize:14]];
        [goodsView addSubview:TiyoncourseNameLabel];
        
        
        
        UIButton * TiyonpayButton = [UIButton buttonWithType:UIButtonTypeCustom];
        TiyonpayButton.layer.cornerRadius = 2;
        TiyonpayButton.layer.masksToBounds = YES;
        TiyonpayButton.tag = 20 + k;
        [TiyonpayButton setTitle:@"评价" forState:UIControlStateNormal];
        [TiyonpayButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [TiyonpayButton addTarget:self action:@selector(replyGoods:) forControlEvents:UIControlEventTouchUpInside];
        TiyonpayButton.titleLabel.font = [UIFont systemFontOfSize:14];
        TiyonpayButton.backgroundColor = [UIColor orangeColor];
        [TiyonpayButton setFrame:CGRectMake(SCREEN_WIDTH-80, 70+ oneOrderH * k, 60, 20)];
        [goodsView addSubview:TiyonpayButton];
        
        
        
        UILabel * TiyonsubCourerLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 60+ k * oneOrderH, SCREEN_WIDTH-75, 20)];
        TiyonsubCourerLabel.numberOfLines=0;
        TiyonsubCourerLabel.adjustsFontSizeToFitWidth=YES;
        [TiyonsubCourerLabel setTextAlignment:NSTextAlignmentLeft];
        [TiyonsubCourerLabel setTextColor:[UIColor darkGrayColor]];
        [TiyonsubCourerLabel setFont:[UIFont systemFontOfSize:13]];
        
        [goodsView addSubview:TiyonsubCourerLabel];
        
        
        
        
        
        
        if ([[NSString stringWithFormat:@"%@",[dic objectForKey:@"is_gift"]] isEqualToString:@"0"]) {
            
            
            //            UILabel *blineLabel = [[UILabel alloc] init];
            //            [blineLabel setFrame:CGRectMake(20, 2+ 55+ k * oneOrderH -1, SCREEN_WIDTH-20, 2)];
            //
            //            [blineLabel setBackgroundColor:RGBCOLOR(231, 236, 237)];
            //            [goodsView addSubview:blineLabel];
            
            TiyoncourseNameLabel.text=[NSString stringWithFormat:@"%@",[dic objectForKey:@"title"]];
            
            productNames =TiyoncourseNameLabel.text;
            [productArray addObject:TiyoncourseNameLabel.text];
           
            
            UILabel *blineLabel = [[UILabel alloc] init];
            [blineLabel setFrame:CGRectMake(20, 40 + k * oneOrderH -2, SCREEN_WIDTH-20, 2)];
            
            [blineLabel setBackgroundColor:RGBCOLOR(231, 236, 237)];
            [goodsView addSubview:blineLabel];
            
            
            
        }
        
        if ([[NSString stringWithFormat:@"%@",[dic objectForKey:@"is_gift"]] isEqualToString:@"1"]) {
            
            TiyonsubCourerLabel.text = [dic objectForKey:@"title"];
            UILabel * TiyonfreeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 60 + k * oneOrderH, 15, 15)];
            TiyonfreeLabel.layer.cornerRadius = 2;
            TiyonfreeLabel.layer.masksToBounds = YES;
            [TiyonfreeLabel setText:@"赠"];
            [TiyonfreeLabel setTextColor:[UIColor whiteColor]];
            [TiyonfreeLabel setTextAlignment:NSTextAlignmentCenter];
            [TiyonfreeLabel setFont:[UIFont systemFontOfSize:14]];
            [TiyonfreeLabel setBackgroundColor:[UIColor redColor]];
            [goodsView addSubview:TiyonfreeLabel];
            
            [TiyonpayButton setFrame:CGRectMake(SCREEN_WIDTH-80, 85+ oneOrderH * k, 60, 20)];
            
            
            
            UILabel *blineLabel = [[UILabel alloc] init];
            [blineLabel setFrame:CGRectMake(20, 60 + k * oneOrderH -2, SCREEN_WIDTH-20, 2)];
            
            [blineLabel setBackgroundColor:RGBCOLOR(231, 236, 237)];
            [goodsView addSubview:blineLabel];
            
            
        }
        
        
        
        k ++;
        
    }
    
    bsvH += 10;
    // ==========添加地址展示控件===========
    UIView *bgViewTwo = [[UIView alloc] initWithFrame:CGRectMake(0, bsvH, SCREEN_WIDTH, SCREEN_HEIGHT/7)];
    //    UIView *bgViewTwo = [[UIView alloc] initWithFrame:CGRectMake(0, oneOrderH * arrayCount+SCREEN_HEIGHT/6+5, SCREEN_WIDTH, SCREEN_HEIGHT/7)];
    [bgViewTwo setBackgroundColor:[UIColor whiteColor]];
    [bigScrollView addSubview:bgViewTwo];
    
    bsvH += SCREEN_HEIGHT/7 + 10;
    
    nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 120, 20)];
    //    [nameLabel setText:@"小龙女"];
    [nameLabel setTextAlignment:NSTextAlignmentLeft];
    [bgViewTwo addSubview:nameLabel];
    
    UIImageView *phoneImg = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-125, 10, 15, 20)];
    [phoneImg setImage:[UIImage imageNamed:@"phone.png"]];
    [bgViewTwo addSubview:phoneImg];
    
    
    phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-140, 10, 120, 20)];
    //    [phoneLabel setText:@"13839128942"];
    [phoneLabel setTextAlignment:NSTextAlignmentRight];
    [phoneLabel setTextColor:[UIColor darkGrayColor]];
    [phoneLabel setFont:[UIFont systemFontOfSize:14]];
    [bgViewTwo addSubview:phoneLabel];
    
    addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, SCREEN_WIDTH-40, 30)];
    //    [addressLabel setText:@"中华人民共和国最南领地-钓鱼岛-岛中央1区2楼110室"];
    [addressLabel setTextAlignment:NSTextAlignmentLeft];
    [addressLabel setTextColor:[UIColor darkGrayColor]];
    [addressLabel setFont:[UIFont systemFontOfSize:14]];
    [addressLabel setNumberOfLines:0];
    [bgViewTwo addSubview:addressLabel];
    
    for(int i=0;i<2;i++) {
        //        UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0+78*i, SCREEN_WIDTH, 2)];
        //        [lineLabel setBackgroundColor:[UIColor purpleColor]];
        //        [bgViewTwo addSubview:lineLabel];
        DecorateLine *line = [[DecorateLine alloc] initWithFrame:CGRectMake(0, 78*i, SCREEN_WIDTH, 2)];
        line.backgroundColor = [UIColor clearColor];
        [bgViewTwo addSubview:line];
        
    }
    
    
    
    
    UIView *bgViewFour = [[UIView alloc] initWithFrame:CGRectMake(0, bsvH, SCREEN_WIDTH, SCREEN_HEIGHT/2.5)];
    //    UIView *bgViewFour = [[UIView alloc] initWithFrame:CGRectMake(0, oneOrderH * arrayCount+SCREEN_HEIGHT/6+5+SCREEN_HEIGHT/7+10, SCREEN_WIDTH, SCREEN_HEIGHT/2.5)];
    [bgViewFour setBackgroundColor:[UIColor whiteColor]];
    [bigScrollView addSubview:bgViewFour];
    
    bsvH += SCREEN_HEIGHT / 2.5 ;
    
    UILabel * paLabel =[[UILabel alloc] initWithFrame:CGRectMake(20, 10, 120, 30)];
    paLabel.text=@"支付方式";
    paLabel.font=[UIFont boldSystemFontOfSize:20];
    [bgViewFour addSubview:paLabel];
    
    UILabel * label2 =[[UILabel alloc] initWithFrame:CGRectMake(120, 10, 120, 30)];
    label2.text=@"支付宝支付";
    label2.font=[UIFont systemFontOfSize:15];
    [bgViewFour addSubview:label2];
    
    label3 =[[UILabel alloc] initWithFrame:CGRectMake(20, 40+10, SCREEN_WIDTH, 30)];
    [bgViewFour addSubview:label3];
    
    label4 =[[UILabel alloc] initWithFrame:CGRectMake(0, 40+10+30, SCREEN_WIDTH-20, 30)];
    //    label4.text=@"- 使用学习卡金额：￥0";
    label4.textAlignment=NSTextAlignmentRight;
    [bgViewFour addSubview:label4];
    
    label5 =[[UILabel alloc] initWithFrame:CGRectMake(0, 40+10+30+30, SCREEN_WIDTH-20, 30)];
    //    label5.text=@"- 使用活动学习卡：￥0";
    label5.textAlignment=NSTextAlignmentRight;
    [bgViewFour addSubview:label5];
    
    label6=[[UILabel alloc] initWithFrame:CGRectMake(0, 40+10+30+30+30, SCREEN_WIDTH-20, 30)];
    //    label6.text=@"- 使用优惠劵：￥0";
    label6.textAlignment=NSTextAlignmentRight;
    [bgViewFour addSubview:label6];
    
    
    label7 =[[UILabel alloc] initWithFrame:CGRectMake(0, 40+10+30+30+30+30, SCREEN_WIDTH-20, 30)];
    //    label7.text=@"- 使用大家网论坛金币：￥0";
    label7.textAlignment=NSTextAlignmentRight;
    [bgViewFour addSubview:label7];
    
    label8 =[[UILabel alloc] initWithFrame:CGRectMake(0, 40+10+30+30+30+30+30, SCREEN_WIDTH-20, 30)];
    //    label8.text=@"下单时间：";
    label8.textAlignment=NSTextAlignmentRight;
    label8.font=[UIFont systemFontOfSize:15];
    label8.textColor=[UIColor grayColor];
    [bgViewFour addSubview:label8];
    
    
    bsvH += 10;
    
    // ==========添加支付评价控件===========
    UIView *bgViewThree = [[UIView alloc] initWithFrame:CGRectMake(0,bsvH, SCREEN_WIDTH, 100)];
    //    UIView *bgViewThree = [[UIView alloc] initWithFrame:CGRectMake(0,oneOrderH * arrayCount+ SCREEN_HEIGHT/6+5+SCREEN_HEIGHT/7+10+SCREEN_HEIGHT/2.5+10, SCREEN_WIDTH, 80)];
    [bgViewThree setBackgroundColor:[UIColor whiteColor]];
    [bigScrollView addSubview:bgViewThree];
    
    bsvH += 100;
    
    
    payLabel=[[UILabel alloc] initWithFrame:CGRectMake(10, 15, 150, 20)];
    payLabel.text=@"待支付金额：";
    payLabel.textColor=[UIColor blackColor];
    payLabel.font=[UIFont systemFontOfSize:18];
    [bgViewThree addSubview:payLabel];
    moneyLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 35, 120, 25)];
    moneyLabel.textColor=[UIColor redColor];
    moneyLabel.font=[UIFont systemFontOfSize:17];
    [bgViewThree addSubview:moneyLabel];
    makeButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 110, 15, 90, 40)];
    makeButton.backgroundColor=[UIColor redColor];
    [makeButton setTitle:@"确认订单" forState:(UIControlStateNormal)];
    [makeButton addTarget:self action:@selector(buttonClicked1) forControlEvents:(UIControlEventTouchUpInside)];
    [bgViewThree addSubview:makeButton];
    //    sayButton = [[UIButton alloc] initWithFrame:CGRectMake(240, 15, 90, 40)];
    //    sayButton.backgroundColor=[UIColor orangeColor];
    //    [sayButton setTitle:@"去评价" forState:(UIControlStateNormal)];
    //    [sayButton addTarget:self action:@selector(buttonClicked) forControlEvents:(UIControlEventTouchUpInside)];
    //    [bgViewThree addSubview:sayButton];
    bsvH += 60;
    
    bigScrollView.contentSize = CGSizeMake( 0, bsvH);
    
    
    if ([self.ordelModel.state isEqualToString:@"4"]) {
        titleLabel.text=@"已完成";
        [payButton setTitle:@"去评价" forState:UIControlStateNormal];
        [payButton setBackgroundColor:[UIColor yellowColor]];
        [payButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
        priceTit.text=@"已付款";
        payLabel.hidden=YES;
        moneyLabel.hidden=YES;
        makeButton.hidden=YES;
    }else{
        titleLabel.text=@"待支付";
        [payButton setTitle:@"去付款" forState:UIControlStateNormal];
        [payButton setBackgroundColor:[UIColor redColor]];
        [payButton addTarget:self action:@selector(buttonClicked1) forControlEvents:UIControlEventTouchUpInside];
        priceTit.text=@"应付款";
        
        moneyLabel.text=[NSString stringWithFormat:@"￥%@",self.ordelModel.priceCount];
    }
    
    
}


-(void)replyGoods:(UIButton * )btn{
    
    logdebug(@"去评价");
    
    OrderReplyViewController * orvc = [[OrderReplyViewController alloc] init];
    
    
    NSDictionary * dict = self.ordelModel.course[btn.tag - 20];
    
    orvc.aid = [NSString stringWithFormat:@"%@",dict[@"aid"]]; //[dict[@"aid"] stringValue];
    
    [self.navigationController pushViewController:orvc animated:YES];
    
}

-(void)createData
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"加载中";
    
    
    //获取数据
    NSString *module = @"user";
    NSString *interface = @"orderdetail";
    
    NSString *timeString = [GetTime getTime];
    NSString *hashString = [[NSString stringWithFormat:@"%@/%@/key/%@/time/%@",module,interface,KEY,timeString] md5];
    NSString *urlString = [NSString stringWithFormat:@"%@/laravel/public/%@/%@/hash/%@/appid/1",URL2,module,interface,hashString];
    
    NSDictionary *parameter = @{@"time":timeString,@"oid":self.oid};
    
    
    //    NSLog(@"%@",parameter);
    //    NSLog(@"%@",urlString);
    
    //POST请求
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    
    [manager POST:urlString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dics = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        NSArray * info =[dics objectForKey:@"info"];
        NSArray * arr=[NSArray array];
        for (NSDictionary * dic in info) {
            arr= [dic objectForKey:@"course"];
            
            
            logdebug(@"%@",dics);
            
            label4.text=[NSString stringWithFormat:@"- 使用学习卡金额：￥%@",dic[@"cardval"]];
            label5.text=[NSString stringWithFormat:@"- 使用活动学习卡：￥%@",dic[@"zcardval"]];
            label6.text=[NSString stringWithFormat:@"- 使用优惠劵：￥%ld",[dic[@"quanval"] integerValue]] ;
            
            label7.text=[NSString stringWithFormat:@"- 使用大家网论坛金币：￥%@",dic[@"coinval"]];
            
            
            

            NSDate *date = [NSDate dateWithTimeIntervalSince1970:[[dic objectForKey:@"stime"] integerValue]];
             NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSString * time = [formatter stringFromDate:date];
            
            
           
            label8.text = [NSString stringWithFormat:@"下单时间：%@",time];
            for (NSDictionary * dic1  in arr) {
                //                numberLabel.text=[NSString stringWithFormat:@"订单号:%@",[dic objectForKey:@"oid"]];
                //                courseNameLabel.text=[dic1 objectForKey:@"title"];
                //                priceLabel.text=[NSString stringWithFormat:@"￥%@",[dic objectForKey:@"priceCount"]];
                nameLabel.text=[dic objectForKey:@"consignee"];
                phoneLabel.text=[dic objectForKey:@"tel"];
                addressLabel.text=[dic objectForKey:@"address"];
                label3.text=[NSString stringWithFormat:@"订单总价：￥%@",[dic objectForKey:@"priceCount"]];
                payNumber=[dic objectForKey:@"priceCount"];
                
                if (arr.count==1) {
                    freeLabel.hidden=YES;
                }else{
                    subCourerLabel.text=[dic1 objectForKey:@"title"];
                    
                }
                
                
                
                //                if ([[dic objectForKey:@"state"] isEqual:@4]) {
                //                    titleLabel.text=@"已完成";
                //                    [payButton setTitle:@"去评价" forState:UIControlStateNormal];
                //                    [payButton setBackgroundColor:[UIColor yellowColor]];
                //                    [payButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
                //                    priceTit.text=@"已付款";
                //                    payLabel.hidden=YES;
                //                    moneyLabel.hidden=YES;
                //                    makeButton.hidden=YES;
                //                }else{
                //                    titleLabel.text=@"待支付";
                //                    [payButton setTitle:@"去付款" forState:UIControlStateNormal];
                //                    [payButton setBackgroundColor:[UIColor redColor]];
                //                    [payButton addTarget:self action:@selector(buttonClicked1) forControlEvents:UIControlEventTouchUpInside];
                //                    priceTit.text=@"应付款";
                //                    moneyLabel.text=[NSString stringWithFormat:@"￥%@",[dic objectForKey:@"priceCount"]];
                //                }
                
                
            }
        }
        
        
        [hud hide:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        logdebug(@"%@",error.localizedDescription);
        [hud hide:YES];
    }];
    
}

/**
 去评价
 */
- (void)buttonClicked {
    NSLog(@"去评价");
    
    OrderReplyViewController * orvc = [[OrderReplyViewController alloc] init];
    orvc.aid=self.aid;
    [self.navigationController pushViewController:orvc animated:YES];
    
}


- (void)buttonClicked1 {
    NSLog(@"去付款");
    
        
        
    /*
     *商户的唯一的parnter和seller。
     *签约后，支付宝会为每个商户分配一个唯一的 parnter 和 seller。
     */
    
    /*============================================================================*/
    /*=======================需要填写商户app申请的===================================*/
    /*============================================================================*/
    NSString *partner = @"2088911290438885";
    NSString *seller = @"2928574672@qq.com";
    NSString *privateKey = @"MIICXQIBAAKBgQDtQz7mv0NiToJNsyCUqJ+nuRG39eu4SjCA6aKx35Lb+knV8Kbvk8O8rShk3pU1Dv8lzTTK8L/OMispgCEYaY8J8CysmR6ZXG0YpkiZGPwgYBN1G/QU5ll+9B+cFO9tuuKcBbiVXSAKw6ZtLbY6g4TNCjYA15fwtEX741wETAGEywIDAQABAoGBAJJGJv1zZUOySztD+A4ud1+cBYR4pL6VXpnbcGZhA9WC1QqEtyZ03vPvfTvSzgJMIJlZjfafvmCNJXcMe1rlmTzDWv3NTYzKL34QS/FEfNiyC8SEk2dPegn+BBDTMfePUwX36akXc/uGXOamd1O6NYZzO82cvTq2HEDFlNNPlNaBAkEA+tIf5Y7m2wk2Rj0qnoWCsL9MBmC51RV08ZHDR5+4uI0EJ0W6Dfp9b+PZCLwqE7G+FMJkywtaKzbX51sJUDuJ0QJBAPIpc256RLECgs3feaCERNXzOotMkhIx6KgqDKkaNrYvPVJMbcPmWby7eddi34nYRm6ba2+M3sueflL/74aBb9sCQAdBnPqfL76WfaN6kYkUg7tpTWHB2p53iHKUrPmdfVKZgc1BDyjoXkimZtkB2JXJRp6DITdelLU7WPOwhoxzrIECQQC8cJEIqVCGxcNv3LTSudqu+oTWHRGV4wx+v6n2dRWaE4yOXDcWOEwgV2rqtl/o7klwjLS/q0k7/eujPtHvQw39AkAXr1cZG7wy91zqQkxHM+e6QycXbiNJhwaP9fSWN/wPFbvzu6wyXkgbGCvrmhogjM9NpdCHtTqAwuD09rJZF0mx";
    /*============================================================================*/
    /*============================================================================*/
    /*============================================================================*/
    
    //partner和seller获取失败,提示
    if ([partner length] == 0 ||
        [seller length] == 0 ||
        [privateKey length] == 0)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"缺少partner或者seller或者私钥。"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        return;
    }
    
    

    
    
    Order *order = [[Order alloc] init];
    order.partner = partner;
    order.seller = seller;
    order.tradeNO = self.ordelModel.oid; //订单ID（由商家自行制定）
    
    if (productArray.count==1) {
            order.productName = [NSString stringWithFormat:@"%@",productNames]; //商品标题
            order.productDescription = [NSString stringWithFormat:@"%@",productNames]; //商品描述
    }else{
        order.productName = [NSString stringWithFormat:@"大家网%lu件商品",productArray.count]; //商品标题
        order.productDescription = [NSString stringWithFormat:@"大家网%lu件商品",productArray.count]; //商品描述

    }
    order.amount =self.ordelModel.priceCount; //商品价格
    order.notifyURL = [NSString stringWithFormat:@"%@/laravel/public/payEnd/paysuccess?order_no=%@&pay=%@",URL2,self.ordelModel.oid,self.ordelModel.priceCount]; //回调URL
    
    order.service = @"mobile.securitypay.pay";
    order.paymentType = @"1";
    order.inputCharset = @"utf-8";
    order.itBPay = @"30m";
    order.showUrl = @"m.alipay.com";
    
    //应用注册scheme,在AlixPayDemo-Info.plist定义URL types
    NSString *appScheme = @"xpkc";
    //将商品信息拼接成字符串
    NSString *orderSpec = [order description];
    NSLog(@"orderSpec = %@",orderSpec);
    
    //获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
    id<DataSigner> signer = CreateRSADataSigner(privateKey);
    NSString *signedString = [signer signString:orderSpec];
    //将签名成功字符串格式化为订单字符串,请严格按照该格式
    NSString *orderString = nil;
    if (signedString != nil) {
        orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
                       orderSpec, signedString, @"RSA"];
        
        [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
            NSLog(@"reslut = %@",resultDic);
        }];
        
        
    }
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
